import 'package:df/page/sms/code_response.dart';
import 'package:df/service/api_service.dart';
import 'package:df/utils/logger_utils.dart';
import 'package:df/widgets/dialog_loading.dart';
import 'package:df/widgets/toast_utils.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:df/generated/l10n.dart';


class CodePage extends StatefulWidget {
  @override
  _CodePage createState() => _CodePage();
}

class _CodePage extends State<CodePage> {
  List<CodeData> countries = [];
  late LoadingDialog _loadingDialog;

  @override
  void initState() {
    super.initState();
    _fetchCountryCodeData();
  }

  Future<void> _fetchCountryCodeData() async {
    try {
      CodeResponse response = await ApiService.getCountryCode();
      LogUtils.v("【其他登录】结果= ${response}");
      int code = response.resultCode;
      List<CodeData> data = response.data ?? [];
      LogUtils.v("【其他登录】结果= ${code}");

      if (code == ApiService.kCodeSuccess) {
        setState(() {
          countries = data;
        });
      } else {
        ToastUtils.show(S.current.requestFailed);
      }
    } catch (error) {
      ToastUtils.show(S.current.requestFailed);
    } finally {
      // _loadingDialog.pop();
    }
  }

  @override
  Widget build(BuildContext context) {
    // _loadingDialog = new LoadingDialog( //调用对话框
    //   text: S.of(context).pleaseWait,
    // );
    // showDialog<Null>(
    //     context: context, //BuildContext对象
    //     barrierDismissible: false,
    //     builder: (BuildContext context) {
    //       return _loadingDialog;
    //     });

    return Scaffold(
      appBar: AppBar(
        title: Text(S.current.selectLocation),
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            GoRouter.of(context).pop();
          },
        ),
      ),
      body: ListView.builder(
        itemCount: countries.length,
        itemBuilder: (context, index) {
          return InkWell(
            onTap: () {
              CodeData codeData = countries[index];
              LogUtils.v('点击条目: ${countries[index].label}');
              // 【2/2】去第二个页面，并把数据带回第一个页面
              GoRouter.of(context).pop(codeData);
            },
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(height: 20),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        '${countries[index].value}',
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                      Text('${countries[index].label}'),
                    ],
                  ),
                ),
                SizedBox(height: 20),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 0),
                  child: Divider(height: 1, color: Colors.grey),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}

